<?php

namespace app\modules\Admin\models;

use Yii;

/**
 * This is the model class for table "admin".
 *
 * @property int $adid
 * @property string $adname 名字
 * @property string $adpass 密码
 * @property int|null $status 1正常  2禁用
 * @property string|null $create_time
 * @property string|null $updated_at
 */
class Admin extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{

    public  $authKey;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'admin';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['adname', 'adpass'], 'required'],
            [['status'], 'integer'],
            [['create_time', 'updated_at'], 'safe'],
            [['adname'], 'string', 'max' => 100],
            [['adpass'], 'string', 'max' => 45],
        ];
    }

    // 保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){
            $this->create_time = date("Y-m-d H:i:s");
            $this->status    = 1;
        }
        return parent::beforeSave($insert);
    }//}}}


    public static function findByUsername($username)
    {
       
        $one = Admin::find()->where(['adname'=>$username])->one();
        if($one){
            return $one;
        }
        return null;
    }

    public function validatePassword($password)
    {
        return $this->adpass === $password;
    }

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->adid;
    }

    public static function findIdentity($id)
    {
        $model = Admin::findOne($id);
        return $model ? $model : null;
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }


    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'adid' => 'ID',
            'adname' => '账号',
            'adpass' => '密码',
            'status' => 'Status',
            'create_time' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
}
